const Common = require('./common');
const MovieModel = require ('../models/movie');
const Constant = require('../constant/constant');
// const dateFormat = require('dateformat'); 
const Token = require("../utils/token");
const { off } = require('../app');

//配置对象
let exportObj = {
    info,
};

module.exports = exportObj;

function info(req,res){
    //查询电影信息
    const resObj = Common.clone(Constant.DEFAULT_SUCCESS); //定义一个返回对象
    //定义一个async任务
    let tasks = {
        //解析token
        getInfo:(cb)=>{
            let offset = parseInt(req.body.from);
            let limit = parseInt(req.body.size);
            let whereCondition = {};
            /** 如果查询条件存在，则查询对象增加查询条件 */
            if (req.body.movieTitle){
                whereCondition.movieTitle = req.query.movieTitle;
            }
            MovieModel.findAndCountAll({
                where:whereCondition,
                offset:offset,
                limit:limit,
                order:[['created_at','DESC']],

            }).then(function(result){
                //查询结果处理
                let list = [];
                result.rows.forEach((v,i)=>{
                    let obj = {
                        id:v.id,
                        movieTitle:v.movie_title,
                        movieCover:v.movie_cover,
                        movieDirectory:v.movie_directory,
                    }
                    list.push(obj)
                })
                //给返回结果赋值
                resObj.result ={
                    list,
                    total:result.count,
                }
                cb(null);
            }).catch(function(err){
                console.log(err);
                cb(Constant.DEFAULT_ERROR)
            })
        }

    }
    Common.autoFn(tasks,res,resObj); //执行公共方法中的autoFn方法，返回数据
}